package com.health.controller;

import com.health.pojo.User;
import com.health.service.UserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.List;

@RestController
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;


    @GetMapping("/findAll")
    public Object findAll(){
        //判断redis数据库中是否有,有则返回,无则查询并添加进redis
        Boolean hasKey = redisTemplate.hasKey("userList");
        if (hasKey !=null &&hasKey){
            return redisTemplate.opsForValue().get("userList");
        }
        //数据库查询
        List<User> userList= userService.findAll();
        //添加到redis
        redisTemplate.opsForValue().set("userList",userList, Duration.ofMinutes(10));
        return userList;
    }

}
